<template>
  <div class="pageBox">
    <head-component title="我的订单"></head-component>
    <van-tabs
      v-model:active="active"
      @change="tabChange"
      title-inactive-color="#333333"
      title-active-color="#E00B12"
      line-width="20px"
    >
      <van-tab title="全部" name="0"></van-tab>
      <van-tab title="待付款" name="1"></van-tab>
      <van-tab title="待发货" name="2"></van-tab>
      <van-tab title="待收货" name="3"></van-tab>
      <van-tab title="已完成" name="4"></van-tab>
    </van-tabs>
    <van-list
      v-model:loading="loading"
      :finished="finished"
      finished-text="没有更多了"
      @load="onLoad"
      :immediate-check="false"
    >
      <div class="list">
        <div
          class="listItem"
          @click="goDetails(item.id)"
          v-for="item in list"
          :key="item.id"
        >
          <div class="itemTop flex-between">
            <p>订单编号：{{ item.orderNo }}</p>
            <span v-if="item.status == -1">交易关闭</span>
            <span v-if="item.status == 0">待付款</span>
            <span v-if="item.status == 1">待发货</span>
            <span v-if="item.status == 2">待收货</span>
            <span v-if="item.status == 3">已完成</span>
            <span v-if="item.status == 7">拼团中</span>
            <span v-if="item.status == 8">拼团失败</span>
          </div>
          <div
            class="goodsInfo flex-between"
            v-for="item2 in item.orderGoods"
            :key="item2.id"
          >
            <img class="infoLeft" :src="item2.thumb" alt="" />
            <div class="infoRight">
              <div class="infoTitle">{{ item2.title }}</div>
              <div class="sku flex-row" v-if="item2.specTitle">
                <div class="skuItem">规格：{{ item2.specTitle }}</div>
              </div>
              <div class="price">￥{{ item2.price }}</div>
              <div class="number">x {{ item2.num }}</div>
            </div>
          </div>
          <div class="freight flex-between">
            <p>运费</p>
            <span>￥{{ item.freight }}</span>
          </div>
          <div class="totalBox flex-row">
            <p>共{{ item.num }}件商品，合计</p>
            <span>￥{{ item.finalPrice }}</span>
          </div>
          <div class="btnBox flex-row">
            <button
              class="closeBtn"
              @click.stop="onCloseBtn(item.id)"
              v-if="item.status == 0"
            >
              取消订单
            </button>
            <button class="payBtn" v-if="item.status == 0" @click.stop="onPayBtn(item)">付款</button>
            <button
              class="closeBtn"
              @click.stop="goLogistics(item)"
              v-if="item.status == 2"
            >
              查看物流
            </button>
            <button class="payBtn" v-if="item.status == 2" @click.stop="onFinish(item.id)">确认收货</button>
          </div>
        </div>
      </div>
    </van-list>

    <van-popup v-model:show="show" position="bottom" round>
      <div class="popupBox">
        <img
          class="popupClose"
          src="@/image/my/popupClose.png"
          alt=""
          @click="show = false"
        />
        <div class="title flex-center">请选择原因</div>
        <div class="tips flex-center">
          订单一旦取消，无法恢复，金额金豆将原路返还
        </div>
        <div class="reason">
          <div
            class="reasonItem flex-between"
            v-for="item in closeReason"
            :key="item.id"
            @click="onReasonItem(item)"
          >
            <div
              :class="[
                item.status ? 'reasonItemRActive reasonItemR' : 'reasonItemR',
              ]"
            >
              {{ item.title }}
            </div>
            <img :src="item.chooseImg" alt="" v-if="!item.status" />
            <img :src="item.choosedImg" alt="" v-else />
          </div>
        </div>
      </div>
    </van-popup>
  </div>
</template>

<script type="text/ecmascript-6">
import { Toast } from "vant";
import { defineComponent, onMounted, ref } from "vue";
import headComponent from "../../components/headComponent.vue";
import router from "../../router";
import { post } from "../../untils/request";

export default defineComponent({
  components: {
    headComponent,
  },
  setup() {
    const active = ref(
      router.currentRoute.value.query.type
        ? router.currentRoute.value.query.type
        : ""
    );

    const show = ref(false);

    const reasonId = ref(1);
    const reasonTitle = ref("收货地址填错了");
    const orderId = ref("");
    const closeReason = ref([
      {
        id: 1,
        title: "收货地址填错了",
        status: true,
        chooseImg: require("@/image/my/closeChoose.png"),
        choosedImg: require("@/image/my/closeChoosed.png"),
      },
      {
        id: 2,
        title: "忘记支付密码/余额不足",
        status: false,
        chooseImg: require("@/image/my/closeChoose.png"),
        choosedImg: require("@/image/my/closeChoosed.png"),
      },
      {
        id: 3,
        title: "无法正常支付",
        status: false,
        chooseImg: require("@/image/my/closeChoose.png"),
        choosedImg: require("@/image/my/closeChoosed.png"),
      },
      {
        id: 4,
        title: "不想买了",
        status: false,
        chooseImg: require("@/image/my/closeChoose.png"),
        choosedImg: require("@/image/my/closeChoosed.png"),
      },
      {
        id: 5,
        title: "其他原因",
        status: false,
        chooseImg: require("@/image/my/closeChoose.png"),
        choosedImg: require("@/image/my/closeChoosed.png"),
      },
    ]);

    const page = ref(1);
    const lastId = ref(0);
    const list = ref([]);
    const loading = ref(true);
    const finished = ref(false);

    function onLoad() {
      post("/portal/Shop", {
        module: "Order",
        interface: "1000",
        data: {
          page: page.value,
          lastId: lastId.value,
          status: active.value,
          type: "",
        },
      }).then((res) => {
        console.log("我的订单", res);
        let datas = res.data.list;
        if (datas) {
          list.value = list.value.concat(datas);
          loading.value = false;
        }
        lastId.value = res.data.lastId;
        page.value = res.data.currentPage + 1;
        if (page.value > res.data.lastPage) {
          loading.value = false;
          finished.value = true;
        }
      });
    }

    function tabChange(name) {
      console.log(name, active.value);
      refresh();
    }

    function refresh() {
      page.value = 1;
      lastId.value = 0;
      loading.value = true;
      finished.value = false;
      list.value = [];
      onLoad();
    }

    // 取消订单
    function onCloseBtn(id) {
      orderId.value = id;
      show.value = true;
    }

    function onReasonItem(param) {
      closeReason.value.forEach((item) => {
        item.status = false;
        if (item.id == param.id) {
          item.status = true;
          reasonId.value = item.id;
          reasonTitle.value = item.title;
          close();
        }
      });
    }

    function close() {
      post("/portal/Shop", {
        loading: true,
        module: "Order",
        interface: "1004",
        data: {
          id: orderId.value,
          reason: reasonTitle.value,
        },
      }).then(() => {
        Toast("取消成功");
        show.value = false;
        refresh();
      });
    }

    function goDetails(id) {
      router.push({
        path: "/orderDetails",
        query: {
          id: id,
        },
      });
    }

    function goLogistics(param) {
      router.push({
        path: "/logistics",
        query: {
          param: JSON.stringify(param),
        }
      });
    }

    // 确认收货
    function onFinish(id) {
       post("/portal/Shop", {
        loading: true,
        module: "Order",
        interface: "1003",
        data: {
          id: id,
        },
      }).then(() => {
        Toast("确认收货成功");
        refresh();
      });
    }

    function onPayBtn(param) {
      console.log("付款");
      orderPay(param.orderNo)
    }

    //订单支付 余额支付
    function orderPay(orderNo) {
      post("/portal", {
        loading: true,
        module: "Pay",
        interface: "1000",
        data: {
          order_no: orderNo,
        },
      }).then((res) => {
        console.log("订单支付", res);
        router.replace("/paySuccess");
      });
    }

    onMounted(() => {
      onLoad();
    });

    return {
      active,
      show,
      closeReason,
      reasonId,
      reasonTitle,
      orderId,
      page,
      lastId,
      list,
      loading,
      finished,
      onLoad,
      tabChange,
      onCloseBtn,
      onReasonItem,
      goDetails,
      goLogistics,
      refresh,
      onFinish,
      onPayBtn,
    };
  },
});
</script>

<style scoped lang="less">
.pageBox {
  width: 100%;
  min-height: 100vh;
  background-color: #f5f5f5;
  padding-top: 88px;
  .list {
    .listItem {
      background-color: #fff;
      margin-top: 20px;
      padding: 30px;
      .itemTop {
        p {
          font-size: 24px;
          font-weight: 400;
          color: #999999;
        }
        span {
          font-size: 24px;
          font-weight: 400;
          color: #e00b12;
        }
      }
      .goodsInfo {
        margin-top: 40px;
        width: 100%;
        .infoLeft {
          width: 168px;
          height: 168px;
          border-radius: 10px;
        }
        .infoRight {
          width: 100%;
          padding-left: 20px;
          .infoTitle {
            font-size: 28px;
            font-weight: 500;
            color: #333333;
          }
          .sku {
            flex-wrap: wrap;
            margin-top: 20px;
            .skuItem {
              font-size: 26px;
              font-weight: 400;
              color: #999999;
              margin-right: 14px;
            }
          }
          .price {
            text-align: right;
            font-size: 26px;
            font-weight: 500;
            color: #333333;
          }
          .number {
            text-align: right;
            font-size: 26px;
            font-weight: 400;
            color: #999999;
          }
        }
      }
      .freight {
        margin-top: 30px;
        p {
          font-size: 26px;
          font-weight: 400;
          color: #999999;
        }
        span {
          font-size: 26px;
          font-weight: 400;
          color: #e00b12;
        }
      }
      .totalBox {
        margin-top: 30px;
        justify-content: flex-end;
        p {
          font-size: 26px;
          font-weight: 400;
          color: #999999;
        }
        span {
          font-size: 26px;
          font-weight: 400;
          color: #e00b12;
        }
      }
      .btnBox {
        margin-top: 61px;
        justify-content: flex-end;
        button {
          width: 144px;
          height: 56px;
          border-radius: 10px;
          font-size: 24px;
          font-weight: 400;
          margin-left: 39px;
        }
        .closeBtn {
          color: #999999;
          background: #ffffff;
          border: 1px solid #cccccc;
        }
        .payBtn {
          color: #ffffff;
          background: linear-gradient(270deg, #b30b11 0%, #f50811 100%);
          border: none;
        }
      }
    }
  }
  .popupBox {
    padding: 46px 39px 34px 40px;
    position: relative;
    .popupClose {
      width: 48px;
      height: 48px;
      position: absolute;
      right: 19px;
      top: 19px;
    }
    .title {
      font-size: 36px;
      font-weight: 500;
      color: #333333;
    }
    .tips {
      font-size: 26px;
      font-weight: 400;
      color: #e00b12;
      margin-top: 16px;
      margin-bottom: 7px;
    }
    .reason {
      .reasonItem {
        margin-top: 72px;
        .reasonItemR {
          font-size: 30px;
          font-weight: 400;
          color: #333333;
        }
        .reasonItemRActive {
          font-weight: bold;
        }
        img {
          width: 40px;
          height: 40px;
        }
      }
    }
  }
}
</style>